﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using HZ.DrugTrade.IBLL;
using HZ.DrugTrade.BLLFactory;
using HZ.DrugTrade.Model;

namespace HZ.DrugTrade.Web.Hospitals.Procurement
{
    public partial class PurchaseCompanyUpdate_UnBid : BasePage
    {
        public PurchaseCompanyUpdate_UnBid()
        {
            this.ModuleNo = "";
        }

        IBLL_Distribution id = Factory.BLL_Distribution();
        IBLL_ProcurementBasic pbCtrl = Factory.BLL_ProcurementBasic();
        IBLL_HospitalProcureCatalog hpcCtrl = Factory.BLL_HospitalProcureCatalog();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (!string.IsNullOrEmpty(Request.QueryString["CompanyID_PS"]))
                {
                    hdnCompnayId.Value = Request.QueryString["CompanyID_PS"];
                }
                BindAllCompanyList();
            }
            else
            {
                txtCompanyName.Text = hdnCompanyName.Value;
            }
        }

        /// <summary>
        /// 绑定所有可选的配送企业
        /// </summary>
        private void BindAllCompanyList()
        {
            string procureCatalogId = Request.QueryString["procureCatalogId"];
            string hospitalId = CurrentAccount.GetUser().Org.OrgID;
            //by ligang 2009-6-15
            HZ.DrugTrade.Model.View.ProcureCatalog procureCatalog = pbCtrl.GetProcureCatalogByID(procureCatalogId);
            this.lblGoodsName.Text = procureCatalog.GoodsName;
            this.lblGg.Text = procureCatalog.Outlookc;
            this.lblCompanyName_SC.Text = procureCatalog.CompanyName_SC;
            this.lblCompanyName_ZB.Text = "NA";
            this.lblBidPrice.Text = (procureCatalog.ProcurePrice.Value==0)?"NA":procureCatalog.ProcurePrice.Value.ToString("0.0000");
            //将采购限价默认设置为采购限价，若有采购限价则覆盖
            this.txtPsPrice.Text = (procureCatalog.ProcurePrice.Value == 0) ? "0" : procureCatalog.ProcurePrice.Value.ToString("0.0000");
           


            //获取产品目录配送信息
            int tmpCount;
            HZ.DrugTrade.Model.View.HospitalProcureCatalog hospitalProcureCatalog = hpcCtrl.GetHospitalProcureCatalog(
                new HZ.DrugTrade.Model.Query.HospitalProcureCatalog() { ProcureCatalogID = procureCatalogId, HospitalID = hospitalId }, 1, 0, out tmpCount)[0];

            if (hospitalProcureCatalog != null)
            {
                this.txtPsPrice.Text = (hospitalProcureCatalog.Price??0).ToString("0.0000");
                this.txtCompanyName.Text = hospitalProcureCatalog.CompanyName_PS;
                //设置已设置了配送企业之后能进行二次选择
                if (!string.IsNullOrEmpty(hospitalProcureCatalog.CompanyName_PS))
                {
                 //   this.Page.RegisterStartupScript("", "<script>displaybtn();</script>");
                    this.txtPsPrice.ReadOnly = true;
                  //  this.btnSave.Enabled = false;
                }
               
                this.hdnCompnayId.Value = hospitalProcureCatalog.CompanyID_PS;
                this.hdnCompanyName.Value = hospitalProcureCatalog.CompanyName_PS;
            }
            //int temp;
            //HZ.DrugTrade.Model.Query.DistributionRelation where = new HZ.DrugTrade.Model.Query.DistributionRelation() { 
            //    ProcureCatalogID = procureCatalogId,
            //    HospitalID = CurrentAccount.GetUser().Org.OrgID,
            //    DistributionIsUsing = true,
            //    SortFields = "ProcureCatalogID"
            //};
            //IList<Model.View.DistributionRelation> disrlist = BLLFactory.Factory.BLL_Distribution().GetDistributionByWhere(where, 1, 0, out temp);
            //if (disrlist.Count > 0)
            //{
            //    this.txtPsPrice.Text = disrlist[0].Price.Value.ToString("0.00");
            //    this.txtCompanyName.Text = disrlist[0].CompanyName_PS;
            //    this.hdnCompnayId.Value = disrlist[0].CompanyID_PS;

            //if (disrlist[0].Price.Value == 0)
            //{
            //    txtPsPrice.Attributes.Add("onblur", "if(!confirm('采购限价可能高于最高限价，是否确认无误？')){this.value=0}");
            //}
            //}
        }
       

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSave_Click1(object sender, EventArgs e)
        {

            string strCompanyIDPs = this.hdnCompnayId.Value;
            string strCompanyName_PS = this.hdnCompanyName.Value;//从隐藏字段中获取名字，txt控件Enable为false不会传值回来
            string strPrice = this.txtPsPrice.Text;

            if (new HZ.Common.ConvertHelper(strCompanyIDPs).String == "")
            {
                HZ.Web.WebUtil.Alert("配送企业未选择！", this); 
                return;
            }
            if (!(double.Parse(strPrice)>0))
            {
                HZ.Web.WebUtil.Alert("价格不能为0！", this);
                return;
            }
            if (!HZ.Common.MyValidate.IsMoney(strPrice))
            {
                HZ.Web.WebUtil.Alert("价格格式不对！", this);
                txtPsPrice.Focus();
                return;
            }
            string procureCatalogId = Request.QueryString["procureCatalogId"];
            string hospitalId = CurrentAccount.GetUser().Org.OrgID;
            int tmpCount;
            HZ.DrugTrade.Model.View.HospitalProcureCatalog hospitalProcureCatalog = hpcCtrl.GetHospitalProcureCatalog(
                new HZ.DrugTrade.Model.Query.HospitalProcureCatalog() { ProcureCatalogID = procureCatalogId, HospitalID = hospitalId }, 1, 0, out tmpCount)[0];
            //if (Convert.ToDecimal(strPrice) > hospitalProcureCatalog.ProcurePrice)
            //{
            //    if (hospitalProcureCatalog.ProcurePrice != 0)
            //    {
            //        HZ.Web.WebUtil.Alert("采购限价不能高于采购价" + hospitalProcureCatalog.ProcurePrice.Value.ToString("0.00") + "！", this);
            //        txtPsPrice.Focus();
            //        return;
            //    }
            //}

            //更新配送企业及价格
            Model.HospitalProcureCatalog model = new HospitalProcureCatalog();
            model.HospitalID = CurrentAccount.GetUser().Org.OrgID;
            model.CompanyID_PS = strCompanyIDPs;
            model.CompanyName_PS = strCompanyName_PS;
            model.ProcureCatalogID = Request.QueryString["procureCatalogId"];
            model.Price = Convert.ToDecimal(strPrice);
            if (strCompanyIDPs != hospitalProcureCatalog.CompanyID_PS)
            {
                model.CompanyID_Last = hospitalProcureCatalog.CompanyID_PS;
            }

            int result = hpcCtrl.UpdateHospitalProcureCatalogCompanyPS(model);
            if (result == 1)
            {
                HZ.Web.WebUtil.Alert("设置配送企业成功！", this);
                BindAllCompanyList();
            }
            else
            {
                PageError("设置出错！请联系管理员", this.ReturnUrl);
            }
        }
    }
}
